#
# This makefile builds signal_source project
# The target platform is SBOS running on SandBlaster DSP
#

# Program name.
PROGRAM=signal_source

# Build directory. All intermediate files created while 
# building this project are stored in this directory.
BUILD_DIR=build

# Source files directory
SOURCE_DIR=Sources

# Source files. The Files of the following types are 
# supported: *.c, *.s, *.SS
SOURCE_FILES= \
	main.c

# Pre-built library files
PREBUILT_LIB_FILES=

# SandBlaster linker script for static memory allocation 
ATFILES=

ifeq "$(SBROOT)" ""
$(error "SBROOT environment variable is not defined")
endif

# Make sure SBROOT uses '/' path separator characters
SBROOT:=$(subst \,/,$(SBROOT))

# SandBlaster DSP tools
SBCC="$(SBROOT)/bin/sbcc"
SBAR="$(SBROOT)/bin/sb-ar"
SBNM="$(SBROOT)/bin/sb-nm"
SBOD="$(SBROOT)/bin/sb-objdump"
SBCCFE="$(SBROOT)/lib/sbcc-lib/sbcc-fe"

# Common UNIX command line utilities
ifeq "$(patsubst Windows%,Windows,$(OS))" "Windows" 
	MKDIR="$(SBROOT)/msys/bin/mkdir"
	RM="$(SBROOT)/msys/bin/rm"
	CAT="$(SBROOT)/msys/bin/cat"
else ifeq ($(shell uname),Linux)
	MKDIR=mkdir
	RM=rm
	CAT=cat
endif	

# C compiler flags common to all source files
CFLAGS=--march=sb3500 $(XCFLAGS) --no-wrap-diagnostics

# C preprocessor include directories common to all source files
CPPINC=-ISources

# C preprocessor definitions common to all source files
CPPDEF=$(XCPPDEF)

# C compiler flags passed to the linker
CLDFLAGS=

# Make object file names out of the source files names
SOURCE_TO_OBJECT=$(addprefix $(BUILD_DIR)/,\
$(patsubst %.c,%.o,\
$(patsubst %.SS,%.o,\
$(patsubst %.s,%.o,\
$1))))

# List of the post-processed linker script file names 
# This list is generated by make from $ATFILES
ATFILES_PP=$(foreach f,$(ATFILES),$(BUILD_DIR)/$(notdir $f))

# Write automatically generated dependencies to this file
AUTO_DEPENDENCIES_FILE=Makefile.dep

.PHONY: run

all: $(BUILD_DIR) $(AUTO_DEPENDENCIES_FILE) $(BUILD_DIR)/$(PROGRAM).sbx
	@echo "'$@' complete"

clean:
	-$(RM) -rf $(addprefix $(BUILD_DIR)/*.,o a s v sbx)
	-$(RM) -rf $(BUILD_DIR) $(AUTO_DEPENDENCIES_FILE)
	@echo "'$@' complete"

clobber: clean
	@echo "'$@' complete"

$(BUILD_DIR):
	$(MKDIR) -p $@
	@echo "'$@' complete"

$(BUILD_DIR)/$(PROGRAM).sbx: $(PREBUILT_LIB_FILES) $(ATFILES_PP) \
		$(call SOURCE_TO_OBJECT,$(SOURCE_FILES))
	$(SBCC) --march=sb3500 $(CLDFLAGS) -o $@ \
		$(foreach f,$(filter %.at,$^),-Xld "--at-file=$f") \
		$(filter %.o,$^) \
		$(filter %.a,$^)

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c
	$(SBCC) -c $(CFLAGS) $(CPPINC) $(CPPDEF) -o $@ $<

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.SS
	$(SBCC) -c $(CFLAGS) $(CPPINC) $(CPPDEF) -o $@ $<

$(BUILD_DIR)/%.at: %.at
	$(SBCCFE) -E -P $(CPPDEF) $< >$@

$(BUILD_DIR)/%.d: $(SOURCE_DIR)/%.c
	$(MKDIR) -p $(BUILD_DIR)
	$(SBCC) $(CFLAGS) $(CPPINC) $(CPPDEF) -MM -MQ $(patsubst %.d,%.o,$@) -MM -MQ $@ $< > $@

$(AUTO_DEPENDENCIES_FILE): $(addprefix $(BUILD_DIR)/,$(patsubst %.c,%.d,$(filter %.c,$(SOURCE_FILES))))
	$(CAT) $^ > $@

ifneq ($(firstword $(MAKECMDGOALS)),clean)
ifneq ($(firstword $(MAKECMDGOALS)),clobber)
-include $(AUTO_DEPENDENCIES_FILE)
endif
endif
